﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;
using System.Reflection;
using BlueLotus7.Common;

namespace BlueLotus7.Common
{
    [DataContract]
    public class BaseObject : IBaseObject, ICloneable, ICopyProperty<BaseObject>
    {
        #region Property

        [DataMember]
        public string Name
        {
            get;
            set;
        }

        [DataMember]
        public DateTime CreatedStamp
        {
            get;
            set;
        }

        [DataMember]
        public DateTime LastUpdatedStamp
        {
            get;
            set;
        }

        [DataMember]
        public Guid Key
        {
            get;
            set;
        }

        [DataMember]
        public bool IsDeleted
        {
            get;
            set;
        }

        #endregion

        #region Constructor

        public BaseObject()
        {
        }

        public BaseObject(string serializationData)
        {
            BaseObject obj = DerializeFromXml(this.GetType(), serializationData) as BaseObject;
            if (obj != null)
            {
                CopyProperty(this, obj);
            }
        }

        #endregion

        #region Methods

        public string Serialize()
        {
            return SerializeToXml(this);
        }

        public object Clone()
        {
            BaseObject obj = new BaseObject();
            CopyProperty(obj, this);
            return obj;
        }

        #endregion

        #region Utility

        protected static string SerializeToXml(BaseObject obj)
        {
            StringBuilder xmlBuilder = new StringBuilder();

            if (obj != null)
            {
                XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.OmitXmlDeclaration = false;
                XmlWriter xmlWriter = XmlWriter.Create(xmlBuilder, xmlWriterSettings);
                xmlSerializer.Serialize(xmlWriter, obj);
                xmlWriter.Close();
            }

            return xmlBuilder.ToString();
        }

        protected static BaseObject DerializeFromXml(Type type, string data)
        {
            object result = null;

            if (!string.IsNullOrWhiteSpace(data))
            {
                XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);

                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.OmitXmlDeclaration = false;
                XmlReader xmlReader = XmlReader.Create(data);
                result = xmlSerializer.Deserialize(xmlReader);
                xmlReader.Close();
            }

            return result as BaseObject;
        }

        #endregion

        public virtual void CopyProperty(BaseObject container, BaseObject objectToCopy)
        {
            if (container != null && objectToCopy != null)
            {
                container.CreatedStamp = objectToCopy.CreatedStamp;
                container.Key = objectToCopy.Key;
                container.LastUpdatedStamp = objectToCopy.LastUpdatedStamp;
                container.Name = objectToCopy.Name;
            }
        }



    }
}
